<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>设置密码 - WeChatBot</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
<!-----------------------------------------------------------------------
- Copyright (C) 2025, iwyxdxl
- Licensed under GNU GPL-3.0 or higher, see the LICENSE file for details.
-
- This file is part of WeChatBot.
- WeChatBot is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- WeChatBot is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with WeChatBot.  If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------>
    <style>
        body {
            display: flex;
            flex-direction: column;
            min-height: 100vh;
            margin: 0;
            background-color: #f7f8fc;
            font-family: Arial, sans-serif;
            align-items: center;
            justify-content: center;
            padding: 20px;
            box-sizing: border-box;
        }
        .header {
            width: 100%;
            max-width: 400px;
            margin-bottom: 20px;
        }
        h1 {
            color: #2c3e50;
            font-size: 1.8em;
            text-align: center;
            margin: 0 0 10px 0;
        }
        .setup-container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.08);
            width: 100%;
            max-width: 400px;
            box-sizing: border-box;
            transition: transform 0.2s, box-shadow 0.2s;
        }
        .setup-container:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0,0,0,0.12);
        }
        .error {
            color: #f44336;
            padding: 10px;
            background: #ffe6e6;
            border-radius: 4px;
            margin-bottom: 15px;
            text-align: center;
            font-size: 0.9em;
        }
        .success {
            color: #4CAF50;
            padding: 10px;
            background: #e6f7e6;
            border-radius: 4px;
            margin-bottom: 15px;
            text-align: center;
            font-size: 0.9em;
        }
        .input-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            color: #2c3e50;
            font-weight: bold;
            font-size: 0.9em;
        }
        input[type="password"] {
            width: 100%;
            padding: 12px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 16px;
        }
        input[type="password"]:focus {
            border-color: #4CAF50;
            box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
            outline: none;
        }
        input.invalid {
            border-color: #f44336;
        }
        .password-requirements {
            background-color: #f9f9f9;
            border-left: 4px solid #4CAF50;
            padding: 10px 15px;
            margin-bottom: 15px;
            border-radius: 4px;
            font-size: 0.9em;
        }
        .requirement {
            margin: 5px 0;
            color: #666;
        }
        .requirement.valid {
            color: #4CAF50;
        }
        .requirement.invalid {
            color: #f44336;
        }
        .requirement:before {
            content: '• ';
            font-weight: bold;
        }
        .btn {
            background-color: #4caf50;
            color: white;
            padding: 12px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
            transition: background-color 0.3s;
            width: 100%;
            margin-top: 10px;
            box-sizing: border-box;
        }
        .btn:hover:not(:disabled) {
            background-color: #45a049;
        }
        .btn:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
        .btn-secondary {
            background-color: #6c757d;
            margin-top: 10px;
        }
        .btn-secondary:hover {
            background-color: #545b62;
        }
        .legal-notice {
            margin-top: 30px;
            padding: 15px 20px;
            background-color: #f7f8fc;
            border-top: 1px solid #e0e0e0;
            text-align: center;
            font-size: 0.85em;
            color: #6c757d;
            line-height: 1.5;
            width: 100%;
            max-width: 600px;
            box-sizing: border-box;
        }
        .legal-notice .legal-toggle {
            cursor: pointer;
            color: #4caf50;
            font-weight: bold;
            margin-bottom: 10px;
            display: inline-block;
        }
        .legal-notice .legal-content {
            display: none;
            text-align: left;
            font-size: 0.95em;
        }
        .legal-notice .legal-content a {
            color: #4caf50;
            text-decoration: none;
        }
        .legal-notice .legal-content a:hover {
            text-decoration: underline;
        }
        
        @media (max-width: 480px) {
            h1 {
                font-size: 1.6em;
            }
            .setup-container {
                padding: 20px;
            }
            input[type="password"] {
                padding: 10px;
                font-size: 15px;
            }
            .btn {
                padding: 10px 18px;
                font-size: 15px;
            }
            .legal-notice {
                font-size: 0.8em;
                padding: 10px 15px;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>设置登录密码</h1>
    </div>

    <div class="setup-container">
        {% if error %}
        <div class="error">{{ error }}</div>
        {% endif %}

        {% if success %}
        <div class="success">{{ success }}</div>
        {% endif %}

        <div class="password-requirements">
            <strong>密码要求：</strong>
            <div class="requirement" id="req-length">至少8个字符</div>
            <div class="requirement" id="req-upper">包含大写字母</div>
            <div class="requirement" id="req-lower">包含小写字母</div>
            <div class="requirement" id="req-digit">包含数字</div>
        </div>

        <form method="POST" id="passwordForm">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
            <div class="input-group">
                <label for="password">新密码</label>
                <input type="password" 
                       name="password" 
                       id="password"
                       placeholder="请输入密码"
                       required>
            </div>

            <div class="input-group">
                <label for="confirm_password">确认密码</label>
                <input type="password" 
                       name="confirm_password" 
                       id="confirm_password"
                       placeholder="请再次输入密码"
                       required>
            </div>

            <button type="submit" class="btn" id="submitBtn" disabled>设置密码</button>
        </form>

        {% if allow_skip %}
        <form method="GET" action="{% if return_to == 'config_editor' %}/{% else %}/{% endif %}" style="margin-top: 10px;">
            <button type="submit" class="btn btn-secondary">{% if return_to == 'config_editor' %}取消修改密码{% else %}跳过设置（直接进入编辑器）{% endif %}</button>
        </form>
        {% endif %}
    </div>

    <div class="legal-notice">
        <div class="legal-toggle" id="toggleLegal">显示法律声明</div>
        <div class="legal-content" id="legalContent">
            <p>
                Modified based on the <strong>KouriChat</strong> project.<br>
                原项目版权: Copyright (C) 2025, umaru<br>
                修改版本版权: Copyright (C) 2025, iwyxdxl
            </p>
            <p>
                本软件是自由软件，您可以根据 <a href="https://www.gnu.org/licenses/gpl-3.0.zh-cn.html" target="_blank">
                GNU 通用公共许可证 第3版（或更高版本）</a> 的条款重新发布和修改本软件。
            </p>
            <p>
                本软件按"现状"提供，不提供任何明示或暗示的担保（包括适销性或特定用途适用性）。<br>
                如您未收到完整的许可证副本，请访问 <a href="https://www.gnu.org/licenses/gpl-3.0.zh-cn.html" target="_blank">
                GNU GPL 3.0 中文版</a> 获取。
            </p>
            <p>
                源代码获取：<br>
                原项目源代码：<a href="https://github.com/KouriChat/KouriChat" target="_blank">https://github.com/KouriChat/KouriChat</a><br>
                本项目源代码：<a href="https://github.com/iwyxdxl/WeChatBot_WXAUTO_SE" target="_blank">https://github.com/iwyxdxl/WeChatBot_WXAUTO_SE</a>
            </p>
            <p>
                本软件仅供学习和娱乐使用，请勿用于违法用途，否则产生的一切法律责任均与任何作者无关。
            </p>
        </div>
    </div>

    <script>
        const passwordInput = document.getElementById('password');
        const confirmPasswordInput = document.getElementById('confirm_password');
        const submitBtn = document.getElementById('submitBtn');
        const form = document.getElementById('passwordForm');

        const requirements = {
            length: document.getElementById('req-length'),
            upper: document.getElementById('req-upper'),
            lower: document.getElementById('req-lower'),
            digit: document.getElementById('req-digit')
        };

        function validatePassword(password) {
            const validations = {
                length: password.length >= 8,
                upper: /[A-Z]/.test(password),
                lower: /[a-z]/.test(password),
                digit: /\d/.test(password)
            };

            // 更新UI
            Object.keys(validations).forEach(key => {
                const isValid = validations[key];
                requirements[key].className = `requirement ${isValid ? 'valid' : 'invalid'}`;
            });

            return Object.values(validations).every(v => v);
        }

        function checkPasswordsMatch() {
            const password = passwordInput.value;
            const confirmPassword = confirmPasswordInput.value;
            
            if (confirmPassword && password !== confirmPassword) {
                confirmPasswordInput.classList.add('invalid');
                return false;
            } else {
                confirmPasswordInput.classList.remove('invalid');
                return true;
            }
        }

        function updateSubmitButton() {
            const password = passwordInput.value;
            const confirmPassword = confirmPasswordInput.value;
            
            const passwordValid = validatePassword(password);
            const passwordsMatch = checkPasswordsMatch();
            const bothFilled = password && confirmPassword;
            
            submitBtn.disabled = !(passwordValid && passwordsMatch && bothFilled);
        }

        passwordInput.addEventListener('input', updateSubmitButton);
        confirmPasswordInput.addEventListener('input', updateSubmitButton);

        form.addEventListener('submit', function(e) {
            const password = passwordInput.value;
            const confirmPassword = confirmPasswordInput.value;
            
            if (!validatePassword(password)) {
                e.preventDefault();
                alert('密码不符合要求，请检查密码要求。');
                return;
            }
            
            if (password !== confirmPassword) {
                e.preventDefault();
                alert('两次输入的密码不一致，请重新确认。');
                return;
            }
        });

        // 初始化验证状态
        updateSubmitButton();

        // 如果有重定向URL且显示成功消息，自动跳转
        const urlParams = new URLSearchParams(window.location.search);
        const redirectUrl = urlParams.get('redirect');
        const hasSuccess = document.querySelector('.success');
        
        if (hasSuccess && redirectUrl) {
            setTimeout(function() {
                window.location.href = redirectUrl;
            }, 2000);
        } else if (hasSuccess && '{{ success }}') {
            setTimeout(function() {
                window.location.href = '/login';
            }, 2000);
        }
        
        // 法律声明切换
        document.getElementById('toggleLegal').addEventListener('click', function() {
            var content = document.getElementById('legalContent');
            if (content.style.display === 'none' || content.style.display === '') {
                content.style.display = 'block';
                this.textContent = '隐藏法律声明';
            } else {
                content.style.display = 'none';
                this.textContent = '显示法律声明';
            }
        });
    </script>
</body>
</html>
